package com.hzcx.app.simplechat.ui.main.presenter;

import android.app.Activity;
import android.content.Context;

import com.hzcx.app.simplechat.api.BaseDialogObserver;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.base.BaseEmptyBean;
import com.hzcx.app.simplechat.base.BasePresenter;
import com.hzcx.app.simplechat.bean.UpLoadImgBean;
import com.hzcx.app.simplechat.common.Common;
import com.hzcx.app.simplechat.model.FriendModel;
import com.hzcx.app.simplechat.model.HomeModel;
import com.hzcx.app.simplechat.model.MomentModel;
import com.hzcx.app.simplechat.model.PublicModel;
import com.hzcx.app.simplechat.model.SettingModel;
import com.hzcx.app.simplechat.model.UserModel;
import com.hzcx.app.simplechat.ui.login.bean.DomainNameBean;
import com.hzcx.app.simplechat.ui.main.bean.FriendRequestCountBean;
import com.hzcx.app.simplechat.ui.main.contract.MainContract;
import com.hzcx.app.simplechat.ui.moment.bean.NewMessageCountBean;
import com.hzcx.app.simplechat.ui.publicui.bean.SloganMemberBean;
import com.hzcx.app.simplechat.ui.publicui.bean.UpdateVersionBean;
import com.hzcx.app.simplechat.ui.setting.contract.UpdatePhoneContract;
import com.hzcx.app.simplechat.ui.user.bean.UserInfoBean;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.token.TokenUtils;

public class MainPresenter extends BasePresenter<MainContract.View> implements MainContract.Presenter {

    /**
     * 获取好友请求新消息条数
     *
     * @param context
     */
    @Override
    public void getFriendRequestCount(Context context) {
        FriendModel.getFriendRequestCount(context, new BaseObserver<FriendRequestCountBean>() {
            @Override
            protected void onSuccess(FriendRequestCountBean data) {
                mView.friendRequestCountResult(data);
            }
        });
    }

    /**
     * 获取动态新消息条数
     *
     * @param context
     */
    @Override
    public void getMomentCount(Context context) {
        MomentModel.getNewMessageCount(context, new BaseObserver<NewMessageCountBean>() {
            @Override
            protected void onSuccess(NewMessageCountBean data) {
                mView.momentCountResult(data);
            }
        });
    }

    /**
     * 根据口令获取数据
     *
     * @param context
     * @param slogan
     */
    @Override
    public void getSloganMenber(Context context, String slogan) {
        PublicModel.getSloganMenber(context, slogan, new BaseObserver<SloganMemberBean>() {
            @Override
            protected void onSuccess(SloganMemberBean data) {
                AppHelper.copy(context, "");
                mView.sloganMemberResult(data);
            }
        });
    }

    /**
     * 上传图片
     *
     * @param activity
     * @param url
     */
    @Override
    public void upLoadImg(Activity activity, String url,int tag) {
        PublicModel.upLoadImg(activity, TokenUtils.getTokenHeader(), url, new BaseObserver<UpLoadImgBean>() {
            @Override
            protected void onSuccess(UpLoadImgBean data) {
                mView.upLoadSucces(data.getUrl(),tag);
            }
        });
    }

    /**
     * 上传截图图片
     *
     * @param context
     * @param url
     */
    @Override
    public void riskRecordImg(Context context, String url) {
        PublicModel.riskRecordImg(context, url, new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                mView.riskRecordImgSuccess();
            }
        });
    }

    /**
     * 查询版本更新信息
     *
     * @param context
     */
    @Override
    public void updateVersion(Context context) {
        PublicModel.updateVersion(context, "android", AppHelper.getVersionCode(context), new BaseObserver<UpdateVersionBean>() {
            @Override
            protected void onSuccess(UpdateVersionBean data) {
                mView.updateVersionResult(data);
            }
        });
    }

    /**
     * 收藏图片
     *
     * @param context
     * @param collectdetail
     * @param collectimage
     * @param member_id
     */
    @Override
    public void addChatCollect(Context context, String collectdetail, String collectimage, int member_id) {
        HomeModel.addChatCollect(context, collectdetail, collectimage, member_id, new BaseDialogObserver<BaseEmptyBean>(context) {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                mView.collectSuccess();
            }
        });
    }

    /**
     *  应用到前台-通知服务端
     * @param context
     */
    @Override
    public void putAppOnlineRecord(Context context) {
        PublicModel.putAppOnlineRecord(context, new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                mView.putOnlineSuccess();
            }
        });
    }

    /**
     *  通知服务器自己在线
     * @param context
     */
    @Override
    public void putServiceOnline(Context context) {
        PublicModel.putServiceOnline(context, new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                mView.putServiceOnlineSuccess();
            }
        });
    }

    /**
     *  根据二维码获取用户信息
     * @param context
     * @param url
     */
    @Override
    public void getUserInfoByQrCode(Context context, String url) {
        UserModel.getUserInfoByQrCode(context, url, new BaseDialogObserver<UserInfoBean>(context) {
            @Override
            protected void onSuccess(UserInfoBean data) {
                mView.userInfoResult(data);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                super.onFailure(e, errorMsg);
                mView.onFail();
            }
        });
    }

    /**
     *  上传用户地址
     */
    @Override
    public void putUserLocation(Context context,double lat,double lng,String address) {
        PublicModel.putUserLocation(context, lat, lng, address, new BaseObserver<BaseEmptyBean>() {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                mView.addressPutSuccess();
            }
        });
    }

    /**
     *  根据ID获取用户信息
     * @param context
     * @param id
     * @param isShow
     */
    @Override
    public void getUserInfo(Context context, int id, boolean isShow) {
        UserModel.getUserInfo(context, id, new BaseDialogObserver<UserInfoBean>(context,isShow) {
            @Override
            protected void onSuccess(UserInfoBean data) {
                mView.userInfoResultById(data);
            }
        });
    }

    /**
     *  发送添加好友请求
     * @param context
     * @param member_id
     * @param ver_request
     */
    @Override
    public void sendAddFriendRequest(Context context, int member_id, String ver_request,int type) {
        FriendModel.sendAddFriendRequest(context, member_id, ver_request,type, new BaseDialogObserver<BaseEmptyBean>(context) {
            @Override
            protected void onSuccess(BaseEmptyBean data) {
                mView.sendAddFriendSuccess();
            }
        });
    }

    /**
     *  获取域名配置
     */
    @Override
    public void getDomainNameConfig(Context context) {
        PublicModel.getDomainNameConfig(context, Common.BASE_DOMAIN_NAME, 1, new BaseObserver<DomainNameBean>() {
            @Override
            protected void onSuccess(DomainNameBean data) {
                mView.domainNameResult(data);
            }
        });
    }
}
